<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class SellerStatement extends Model
{
    use SoftDelete;

    protected $name = 'seller_statement';

    protected $autoWriteTimestamp = 'datetime';

    protected $append = ['status_label'];

    public const STATUS_DRAFT = 0;
    public const STATUS_CONFIRMING = 1;
    public const STATUS_WAIT_INVOICE = 2;
    public const STATUS_WAIT_PAYOUT = 3;
    public const STATUS_PAID = 4;

    public static function statusMap(): array
    {
        return [
            self::STATUS_DRAFT => '草稿',
            self::STATUS_CONFIRMING => '确认中',
            self::STATUS_WAIT_INVOICE => '待开票',
            self::STATUS_WAIT_PAYOUT => '待打款',
            self::STATUS_PAID => '已支付',
        ];
    }

    public function items()
    {
        return $this->hasMany(SellerStatementItem::class, 'statement_id');
    }

    public function invoices()
    {
        return $this->hasMany(InvoiceRequest::class, 'statement_id');
    }

    public function payouts()
    {
        return $this->hasMany(Payout::class, 'statement_id');
    }

    public function org()
    {
        return $this->belongsTo(Org::class, 'seller_org_id');
    }

    public function getStatusLabelAttr(): string
    {
        return self::statusMap()[(int) $this->status] ?? '未知状态';
    }
}
